package com.analyst.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    public static Date parseTime(String date1) throws ParseException {
        /*
         * 计算两个时间间隔
         */


        // 创建SimpleDateFormat类型对象、 "yyyy-MM-dd HH:ss:mm.SSS"是正则式，分别表示年月日时分秒毫秒
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        // 将两个String类型的时间转换为Date类型，从而计算差值、parse()方法的作用是将String类型的时间解析为Date类型


        return df.parse(date1);
    }

    public static String toString(Date date) {
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
        return dateformat.format(date);
    }

    /**
     * 把输入的ms转化为*天*时*分*秒的形式
     *
     * @param ms
     * @return
     */
    public static String formatTime(long ms) {
        long days = ms / (1000 * 60 * 60 * 24);
        long hours = (ms % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (ms % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (ms % (1000 * 60)) / 1000;
        long milliSeconds = ms % 1000;

        return String.format("%d天 %d小时 %d分 %d秒 %d毫秒", days, hours, minutes, seconds, milliSeconds);
    }


    public static List<Date> convertDate(List<Date> dates) {
        List<Date> convertedDates = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Date date : dates) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.YEAR, 1900);
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String calenderString = df.format(calendar.getTime());
            try {
                convertedDates.add(df.parse(calenderString));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return convertedDates;
    }

    public static String getIntervalTimeString(Date begin, Date end) {
        long l = end.getTime() - begin.getTime();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        String str = df.format(new Date(l));
        return str;
    }
}
